package ants_utils

import (
	"github.com/panjf2000/ants/v2"
	"log"
)

func AntsRunStopAtFirstError(pool *ants.Pool, tasks []TaskFn) {
	ch := make(chan TaskResult, len(tasks))
	defer close(ch)

	for _, t := range tasks {

		pool.Submit(func() {
			data, err := t()
			ch <- TaskResult{
				Data: data,
				Err:  err,
			}
		})
	}

	count, max := 0, len(tasks)
	for {
		if count >= max { // 全部执行完了返回
			break
		}

		select {
		case r := <-ch:
			count++
			if r.Err != nil { // 第一个错误就返回
				count = 10
				log.Printf("任务执行错误: %v", r.Err)
			}

		}
	}

}
